package com.junzhiit.nengji.shop.web.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.junzhiit.nengji.shop.web.util.FileUtil;
import com.junzhiit.nengji.shop.web.util.aliyun.AliyunOSSClientUtil;
import com.junzhiit.nengji.shop.web.vo.ResultVO;

import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping("/upload")
public class UploadController extends BaseController {

	@Value("${upload.temp}")
	private String tempPath;
	
	//阿里云API的bucket名称
	@Value("${oss.backet.name}")
	private String backetName;

	@ApiOperation(value = "上传图片")
	@RequestMapping(method = RequestMethod.POST)
	public String upload(MultipartFile file) {
		//String uploadName = FileUtil.upload(file, tempPath);
		try {
			String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
			String fileName = UUID.randomUUID().toString().replaceAll("-", "") + suffix;
			String uploadName = AliyunOSSClientUtil.uploadObject2OSS(file.getInputStream(), fileName, backetName, tempPath);
			return uploadName;
		} catch (Exception e) {
			// TODO: handle exception
		}
		return null;
	}
	
	@ApiOperation(value = "删除图片")
	@RequestMapping(value = "/delete",method = RequestMethod.POST)
	public ResultVO delete(String filefullPath) {
		ResultVO result = new ResultVO();
		boolean flag = FileUtil.removeFile(tempPath + filefullPath);
		result.setData(flag);
		return result;
	}
}
